<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-item label="起始班次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                  type="list"
                  v-model="model.shiftInit"
                  :trigger-change="true"
                  dictCode="shift"
                  placeholder="请选择起始班次" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="班制" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                  type="list"
                  v-model="model.shiftType"
                  :trigger-change="true"
                  dictCode="shift_type"
                  placeholder="请选择班制" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                  type="list"
                  v-model="model.workingProcedure"
                  :trigger-change="true"
                  dictCode="procedure"
                  placeholder="请选择工序" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="8">
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                  placeholder="请选择开始时间"
                  v-model="model.startTime"
                  :trigger-change="true"
                  dateFormat="YYYY-MM-DD"
                  style="width: 100%"
                  :disabled-date="disabledDateStart"
                  @openChange="openChange"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                  placeholder="请选择结束时间"
                  v-model="model.endTime"
                  :trigger-change="true"
                  dateFormat="YYYY-MM-DD"
                  style="width: 100%"
                  :disabled-date="disabledDateEnd"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                  type="list"
                  v-model="model.workshop"
                  :trigger-change="true"
                  dictCode="workshop"
                  placeholder="请选择车间" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-show="model.shiftType==2||model.shiftType==3">
          <a-col :span="8">
            <a-form-item label="甲班班教" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('aCoach')">{{ aCoachName }}</div>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="甲班班长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('aMonitor')">{{ aMonitorName }}</div>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-show="model.shiftType==2||model.shiftType==3">
          <a-col :span="8">
            <a-form-item label="乙班班教" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('bCoach')">{{ bCoachName }}</div>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="乙班班长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('bMonitor')">{{ bMonitorName }}</div>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-show="model.shiftType==3">
          <a-col :span="8">
            <a-form-item label="丙班班教" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('cCoach')">{{ cCoachName }}</div>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="丙班班长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="personSelect" @click="handlePerson('cMonitor')">{{ cMonitorName }}</div>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <PersonModal ref="modalForm" @modalFormOk="modalFormOk"></PersonModal>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import PersonModal from './PersonModal'
import moment from 'moment'

export default {
  components: { JFormContainer, JDate, PersonModal },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      disabledDateArr:[],
      form: this.$form.createForm(this),
      model: {},
      columns: [
        'shiftInit',
        'shiftType',
        'workingProcedure',
        'aCoach',
        'aMonitor',
        'bCoach',
        'bMonitor',
        'cCoach',
        'cMonitor'
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 10 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        workingProcedure: {
          rules: [
            { required: true, message: '请选择工序!' }
          ]
        },
        productName: {
          rules: [
            { required: true, message: '请输入品名!' }
          ]
        },
        productFullName: {
          rules: [
            { required: true, message: '请输入品种全称!' }
          ]
        }
      },
      url: {
        add: '/crm_shift/crmShift/add', //新增
        edit: '/crm_shift/crmShift/edit', //编辑
        queryById: '/crm_shift/crmShift/list' //编辑前查询填入
      }
    }
  },
  computed: {
    aCoachName() {
      return this.model.aCoachName ? this.model.aCoachName : '请选择'
    },
    aMonitorName() {
      return this.model.aMonitorName ? this.model.aMonitorName : '请选择'
    },
    bCoachName() {
      return this.model.bCoachName ? this.model.bCoachName : '请选择'
    },
    bMonitorName() {
      return this.model.bMonitorName ? this.model.bMonitorName : '请选择'
    },
    cCoachName() {
      return this.model.cCoachName ? this.model.cCoachName : '请选择'
    },
    cMonitorName() {
      return this.model.cMonitorName ? this.model.cMonitorName : '请选择'
    },
  },
  methods: {
    handlePerson(type) {
      this.$refs.modalForm.add(type)
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    modalFormOk(record) {
      this.$set(this.model, `${record.type}Name`, record.data.name)
      this.$set(this.model, `${record.type}`, record.data.jobNo)
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.columns))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.columns))
    },
    openChange(status){
      console.log('status==',status)
      httpAction('/crm_shift/crmShift/checkDuplicateDate', this.model, 'post').then((res) => {
        if(res.code==200){
          this.disabledDateArr = res.result
        }
      })
    },
    disabledDateStart(current){
      const arr = this.disabledDateArr;
      return !(arr.indexOf(current.format('YYYY-MM-DD'))==-1);
    },
    disabledDateEnd(current){
      const arr = this.disabledDateArr;
      if(current.isBefore(moment(this.model.startTime))){
        return true;
      }
      if(arr.indexOf(current.format('YYYY-MM-DD'))!=-1){
        return true;
      }
      return false
    }

  }
}
</script>

<style scoped>
.personSelect {
  width: 100%;
  height: 32px;
  padding: 4px 11px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  line-height: 1.5;
  color: #c4c2c2;
}
</style>